<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="6">
        <div class="form-item">
          <span class="title">礼品名称</span>
          <el-input placeholder="请输入名称" v-model="param.data.name"></el-input>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="form-item button-container">
          <el-button type="primary" @click="search" icon="el-icon-search">查询</el-button>
          <el-button type="primary" @click="resetSearchCotnet" icon="el-icon-refresh">重置</el-button>
          <el-button type="primary" @click="hadleAddGift" icon="el-icon-circle-plus-outline" v-if="isPermissionsBtn('gift_add')">新增礼品</el-button>
        </div>
      </el-col>
    </el-row>

    <div class="table-body">
      <el-table :data="tableData" v-loading="isLoading" border style="width: 100%">
        <el-table-column prop="name" label="礼品名称"></el-table-column>
        <el-table-column prop="type" label="类型" :formatter="(row) => { return row.type == 1 ? '商城商品' : '非商城商品' }"></el-table-column>
        <el-table-column prop="url" label="图片">
          <template slot-scope="scope">
            <template>
              <el-image class="banner-image" fit="cover" :src="scope.row.pic"></el-image>
            </template>
          </template>
        </el-table-column>
        <el-table-column prop="stock" label="库存"></el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="primary" size="small" v-if="isPermissionsBtn('gift_update')">修改</el-button>
            <el-popconfirm title="确定删除此礼品？" @onConfirm="handleConfirmDelete(scope.row)">
              <el-button style="margin-left:15px" type="danger" size="small" slot="reference" v-if="isPermissionsBtn('gift_del')">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          @current-change="currentPageChange"
          layout="prev, pager, next"
          :total="total"
        ></el-pagination>
      </div>
      <el-dialog title="新增礼品" :visible.sync="isVisible" width="40%">
        <AddGift @submit="submitevent" v-if="isVisible" />
      </el-dialog>
      <el-dialog title="修改礼品" :visible.sync="isVisible1" width="40%">
        <GiftDetail :data="adrow" @submit="updateEvent" v-if="isVisible1" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import AddGift from "./AddGift";
import GiftDetail from "./GiftDetail";
import { mapGetters } from "vuex";
export default {
  name: "",
  data() {
    return {
      isLoading: false,
      tableData: [],
      param: {
        data: {
          name: "" // 名称
        },
        queryRequest: {
          pageNum: 1,
          pageSize: 10
        }
      },
      total: 1,
      isVisible:false,
      adrow: "",
      isVisible1:false
    };
  },
  components: {
      AddGift,
      GiftDetail
  },
  computed: {
       ...mapGetters(["isPermissionsBtn"])
  },
  beforeMount() {},
  mounted() {
    this.initData();
  },
  methods: {
    initData() {
      this.isLoading = true;
      this.$service.tzGiftSelectTzGift(this.param).then(res => {
        this.isLoading = false;
        if (res.code == "00000") {
          if (
            res.data.rows.length == 0 &&
            this.param.queryRequest.pageNum != 1
          ) {
            this.param.queryRequest.pageNum -= 1;
            this.initData();
          }
          this.tableData = res.data.rows;
          this.total = res.data.total || 0;
        }
      });
    },

    search() {
      this.param.queryRequest.pageNum = 1;
      this.initData();
    },
    resetSearchCotnet() {
      //重置搜索条件
      this.param.data.name = "";
    },
    currentPageChange(e) {
      this.param.queryRequest.pageNum = e;
      this.initData();
    },
    hadleAddGift() {
        this.isVisible =true;
    },
     handleClick(row) {
      this.adrow = row.id;
      this.isVisible1 = true;
    },
    handleConfirmDelete(scope) {
      this.$service.tzGiftUpdateStatus(scope.id).then(res => {
        if (res.code == "00000") {
          this.$message({
            message: "删除成功",
            type: "success"
          });
          this.initData();
        } else {
          this.$message({
            message: res.message,
            type: "warning"
          });
        }
      });
    },
     submitevent() {
      this.isVisible = false;
      this.initData();
    },
    updateEvent() {
      this.isVisible1 = false;
      this.initData();
    }
  },
  watch: {}
};
</script>
<style scoped>
.banner-image {
  width: 150px;
}
</style>
